package com.woniuxy.controller;

import com.mysql.jdbc.log.Log;
import com.woniuxy.entity.*;
import com.woniuxy.service.DoctorService;
import com.woniuxy.service.PatientService;
import com.woniuxy.service.RegisterMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
public class RegisterMessageController {

    @Autowired
    RegisterMessageService registerMessageService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    PatientService patientService;

    @GetMapping("/getRegMessageAndPatients/{pageIndex}/{pageSize}")
    public List<RegMessageAndPatient> selectRegMessageAndPatients(@PathVariable String pageIndex,
                                                                  @PathVariable String pageSize) {
        Integer index = Integer.parseInt(pageIndex);
        Integer size = Integer.parseInt(pageSize);
        List<RegMessageAndPatient> messageAndPatients = registerMessageService.selectRegMessageAndPatients(index, size);
        return getRegMessageAndPatients(messageAndPatients);
    }

    @GetMapping("/getRegMessageAndPatients/{pageIndex}/{pageSize}/{patientName}")
    public List<RegMessageAndPatient> selectRegMessageAndPatientsByName(@PathVariable String pageIndex,
                                                                        @PathVariable String pageSize,
                                                                        @PathVariable String patientName) {
        Integer index = Integer.parseInt(pageIndex);
        Integer size = Integer.parseInt(pageSize);
        List<RegMessageAndPatient> messageAndPatients = registerMessageService.
                selectRegMessageAndPatientsByPatientName(index, size,patientName);
        return getRegMessageAndPatients(messageAndPatients);
    }

    @GetMapping("/getRegMessageAndPatientsByDoctorName/{pageIndex}/{pageSize}/{doctorName}")
    public List<RegMessageAndPatient> getRegMessageAndPatientsByDoctorName(@PathVariable String pageIndex,
                                                                        @PathVariable String pageSize,
                                                                        @PathVariable String doctorName) {
        Integer index = Integer.parseInt(pageIndex);
        Integer size = Integer.parseInt(pageSize);
        List<RegMessageAndPatient> messageAndPatients = registerMessageService.
                selectRegMessageAndPatientsByDoctorName(index, size,doctorName);
        return getRegMessageAndPatients(messageAndPatients);
    }

    /*
    * 获取所有的挂号信息，可添加条件
    * */
    @PostMapping("/getAllRegisterMessage/{pageIndex}/{pageSize}")
    public ResponseResult getAllRegisterMessage(
            @RequestBody RegisterMessage registerMessage,
            @PathVariable int pageIndex,
            @PathVariable int pageSize) {
        log.info("进入registerMessage");
        PageBean pageBean = registerMessageService.getAllRegisterMessage(registerMessage, pageIndex, pageSize);
        ResponseResult<PageBean> responseResult = new ResponseResult<>();
        responseResult.setData(pageBean);
        responseResult.setCode(200);
        responseResult.setMsg("success");
        log.info("完成registerMessage");
        return responseResult;
    }

    /*
     * 修改挂号单信息
     * */
    @PutMapping("/modifyRegisterMessage")
    public ResponseResult modifyRegisterMessage(@RequestBody RegisterMessage registerMessage){
        registerMessageService.modifyRegisterMessage(registerMessage);
        ResponseResult responseResult=new ResponseResult(200,"success",null);
        return responseResult;
    }

    //得到一个订单号
    @GetMapping("/registerNo/{len}")
    public String getRegisterNo(@PathVariable int len) {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);

    }

    //加入一个挂号单
    @PostMapping("/addRegMessage")
    public void addRegMessage(@RequestBody FormData formData) {
        System.out.println("****************");
        System.out.println(formData);
        System.out.println("****************");

        RegisterMessage message = new RegisterMessage();
        message.setRegisterId(0);
        message.setRegisterNo(formData.getRegNo());

        Integer patientId = patientService.selectPatientByName(formData.getName()).getPatientId();
        message.setPatientId(patientId);

        long time = System.currentTimeMillis();
        Date date = new Date(time);
        message.setRegisterTime(date);

        message.setRegisterCost(formData.getChargeNum());

        Integer doctorId = doctorService.selectDoctorByName(formData.getDoctorValue()).getDoctorId();
        message.setDoctorId(doctorId);

        Integer handleId = doctorService.selectDoctorByName(formData.getHandle()).getDoctorId();
        message.setCreatorId(handleId);

        message.setStatus(1);

        registerMessageService.insertRegisterMessage(message);
    }

    @GetMapping("getMesCount")
    public Integer selectRegMessageAndPatientsCount() {
        return  registerMessageService.selectRegMessageAndPatientsCount();
    }

    @GetMapping("getMesCountByPatientName/{pageSize}/{patientName}")
    public Integer selectCountByPatientName(@PathVariable String pageSize,
                                            @PathVariable String patientName){
        Integer size = Integer.parseInt(pageSize);
        return  registerMessageService.selectCountByPatientName(size,patientName);
    }

    @GetMapping("getMesCountByDoctorName/{pageSize}/{doctorName}")
    public Integer selectCountByDoctorName(@PathVariable String pageSize,
                                            @PathVariable String doctorName){
        Integer size = Integer.parseInt(pageSize);
        return  registerMessageService.selectCountByDoctorName(size,doctorName);
    }

    private List<RegMessageAndPatient> getRegMessageAndPatients(List<RegMessageAndPatient> messageAndPatients) {
        //通过医生id查询对应医生名和部门名
        for (RegMessageAndPatient messageAndPatient : messageAndPatients) {

            Integer doctorId = messageAndPatient.getDoctorId();
            DoctorAndDepartment doctorAndDepartment = doctorService.selectDoctorMessageById(doctorId);

            messageAndPatient.setDoctorName(doctorAndDepartment.getDoctorName());
            messageAndPatient.setDepartmentName(doctorAndDepartment.getDepartmentsName());
        }
        return messageAndPatients;
    }
}
